<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="/WEB-INF/common/taglibs.jsp" %>
<div class="main-content">
    <div class="main-content-inner">
        <%@include file="/WEB-INF/common/back/breadcrumb.jsp" %>
        <div class="page-content">
            <div class="alert alert-block alert-success">
                请选择
                <strong class="green">
                    ${role.roleName}
                </strong>
                拥有的菜单
            </div>
            <ul id="treeDemo" class="ztree"></ul>
            <div class="clearfix form-actions">
                <div class="col-md-offset-3 col-md-9">
                    <button class="btn btn-info" type="button" onclick="saveMenu()">
                        <i class="ace-icon fa fa-check bigger-110"></i>
                        保存
                    </button>
                    <button class="btn btn-grey" onclick="history.back()">
                        <i class="ace-icon fa fa-arrow-left bigger-110"></i>
                        返回
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var zTreeObj;
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    var setting = {
        check: {
            enable: true
        }
    };
    $.ajax({
        dataType: "json",
        url: "${contextPath}/sys/role/menuTree",
        type: "post",
        data: {roleId: '${role.id}'},
        success: function (data) {
            zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, data);
        }
    });

    function saveMenu() {
        var nodes = zTreeObj.getCheckedNodes(true);
        var addIds = "";
        for (var i = 0; i < nodes.length; i++) {
            addIds += nodes[i].id + ",";
        }
        $.ajax({
            type: "POST",
            url: "${contextPath}/sys/role/updateRoleMenu",
            data: {"addIds": addIds, "roleId": '${role.id}'},
            success: function (result) {
                if (result.success) {
                    window.location.href = "${contextPath}/sys/role/home";
                }
                else {
                    layer.msg(result.msg);
                }
            }
        });
    }
</script>